package Day23;


import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class FuDemo01 {
    //服务器
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ServerSocket server = new ServerSocket(65535);//启动服务器
        System.out.println("服务器启动完毕");
        while (true) {
            Socket socket = server.accept();//等待连接
            System.out.println("连接成功");
            InputStream a = socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(a);
            String s = null;
            try {
                s = (String)ois.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            if("add".equals(s)){
                printView(socket,"添加界面");
                input(socket);
            }else if("delete".equals(s)){
                printView(socket,"删除界面");
                input(socket);
            }else if("revise".equals(s)){
                printView(socket,"删除界面");
                input(socket);
            }else if("printAll".equals(s)){
                printView(socket,"遍历界面");
                output(socket);
            }else if("user".equals(s)){
                printView(socket,"用户界面");
                input(socket);
            }else{
                printView(socket,"抱歉，输入错误");
            }
//            input(socket);
//            output(socket);
//            new Thread(){
//                @Override
//                public void run() {
//                    try {
//                        OutputStream os = socket.getOutputStream();
//                        PrintStream ps = new PrintStream(os);
//                        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\Users\\刘娟\\Desktop\\a.txt"));
//                        ps.println(ois);
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }.start();
//            new Thread(){
//                @Override
//                public void run() {
//                    try {
//                        InputStream is = socket.getInputStream();
//                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
//                        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\刘娟\\Desktop\\a.txt"));
//                        oos.writeObject(br);
//                        oos.close();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }.start();

        }
    }
    private static void input(Socket socket) throws IOException, ClassNotFoundException {
        InputStream is = socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        ArrayList<Express> list = new ArrayList<>();
        list = (ArrayList<Express>) ois.readObject();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\刘娟\\Desktop\\a.txt"));
        oos.writeObject(list);
        oos.close();
        ois.close();
        is.close();
    }

    private static void output(Socket socket) throws IOException{
        OutputStream os = socket.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\Users\\刘娟\\Desktop\\a.txt"));
        oos.writeObject(ois);
//        os.close();
//        oos.close();
//        ois.close();
    }

    private static void printView(Socket socket,String s) throws IOException {
        OutputStream os =  socket.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(s);
//        oos.close();
//        os.close();
    }

}
